'''
Created on 31/07/2011

@author: Digao
'''
def toInt(arg):
    return int(arg)

def toStr(a,b):
    return str(a)+' '+str(b)

input = open('input.in')
CASES = int(input.readline())
for case in range(1,CASES+1):
    NF = int(input.readline().strip())
    MC = int(input.readline().strip())
    custs = []
    for i in range(MC):
        v = map(toInt,input.readline().strip().split())[1:]
        fim = []
        for j in range(0,len(v),2):
            fim.append((v[j],v[j+1]))
        custs.append(fim)
        
    flavors = [0 for i in range(NF+1)]
    sat = {}
    hadChange=True
    isPossible=True
    while hadChange:
        hadChange=False
        for i in range(MC):
            if i in sat:
                continue
            v = custs[i]
            if len(v)==1 and v[0][1]==1:
                flavors[v[0][0]]=1
                sat[i] = 1
                hadChange=True
                break
            else:
                satisfied=False
                for fi in range(len(v)):
                    if flavors[v[fi][0]]==v[fi][1]:
                        satisfied=True
                        break
                if not satisfied:
                    for fi in range(len(v)):
                        if v[fi][1]==1:
                            sat[i]=1
                            flavors[v[fi][0]]=1
                            satisfied=True
                            hadChange=True
                            break
                if not satisfied:
                    isPossible=False
    ans = isPossible and reduce(toStr,flavors[1:]) or 'IMPOSSIBLE'
    print 'Case #%d: %s'%(case,ans)
    


